#!/usr/bin/perl
# 
# Exercise 8.2
# 
# Write a program that can search by name for a gene in an unsorted array.
#
# Answer to Exercise 8.2

use strict;
use warnings;

my @genes = qw(
xgene1 agene2 mgene3 zgene4 cgene5
);

print "Enter the gene name: ";
my $query = <STDIN>;
chomp $query;

my $foundflag = 0;

foreach (@genes) {

print $_, "\n";
	# look for exact name, so anchor to beginning and end of string
	if(/^$query$/) {

		print "Gene $query is known!\n";

		$foundflag = 1;

		# exit the "foreach" loop when you find the gene
		last;
	}
}

unless($foundflag) {

	print "Gene $query is not known!\n";
}

exit;

# # Another "built-in" way to do the same thing is by using the "grep" function.
# # This is a shorter program, and runs faster, too:
# 
# use strict;
# use warnings;
# 
# my @genes = qw(
# xgene1 agene2 mgene3 zgene4 cgene5
# );
# 
# print "Enter the gene name: ";
# my $query = <STDIN>;
# chomp $query;
# 
# 
# if(grep(/^$query$/, @genes)) {
# 	print "Gene $query is known!\n";
# }else{
# 	print "Gene $query is not known!\n";
# }
# 
# exit;
